import React, { Component } from 'react'
import { connect } from 'dva';
import { Form, Input, DatePicker } from 'antd';
import { SearchRow, SearchCol, SearchButton } from '@/styled/form';
import { DICT } from '../enum';
import { DropDownSelect } from '@/components/DropDownSelect/index';

interface Props {
  current: number,
  form: any,
  dispatch?: Function,
  initCurrent: Function,
  dict?: Object,
  cond?: any
}

const FormItem = Form.Item;
const ONE_PAGE_SIZE = 10;

@connect(({ unit_task_check }) => ({
  dict: unit_task_check.dict,
  cond: unit_task_check.cond
}))
class Search extends Component<Props, any> {

  componentDidMount() {
    const { dispatch } = this.props;

    dispatch({
      type: 'unit_task_check/queryDict',
      payload: {
        keyList: DICT
      }
    }).then(() => {
      const { cond, form } = this.props;
      const {pageNum, pageSize, ...searchParams} = cond;
      form.setFieldsValue(searchParams);
    });
  }

  search = e => { e.preventDefault();
    const { dispatch } = this.props;
    this.props.form.validateFields((err, values) => {
      if (!err) {
        dispatch({
          type: 'unit_task_check/updateCond',
          payload: {
            targetTypeList: ['1', '2'],
            ...values
          }
        });

        this.props.initCurrent()
      }
    });
  };

  render() {
    const { form, dict = {} } = this.props;
    const { getFieldDecorator } = form;
    const formItemLayout = {
      labelCol: { span: 8 },
      wrapperCol: { span: 16 },
    };
    return (
      <Form onSubmit={this.search}>
        <SearchRow>
          <SearchCol key={2} span={6}>
            <FormItem
              label="状态"
              {...formItemLayout}
            >
              {getFieldDecorator('status')(
                DropDownSelect(dict, 'task.rule.target.status', '', '160px')
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={3} span={6}>
            <FormItem
              label="任务状态"
              {...formItemLayout}
            >
              {getFieldDecorator('taskStatus')(
                DropDownSelect(dict, 'task.state', '', '160px')
              )}
            </FormItem>
          </SearchCol>
          <SearchCol key={4} span={10}>
            <SearchButton htmlType="submit">搜索</SearchButton>
            <SearchButton onClick={(e) => {
              this.props.form.resetFields();
              this.search(e)
            }} style={{ 'marginLeft': '15px' }}>重置</SearchButton>
          </SearchCol>
        </SearchRow>
      </Form>
    );
  }
}

const SearchS = Form.create<Props>()(Search);
export default SearchS;
